/**
 * hook_lib.c - 钩子框架的共享库入口点
 *
 * 这个文件提供一个简单的封装，使框架可以作为共享库被加载
 * 使用LD_PRELOAD方式动态注入到其他程序中
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>

#include "../include/hook_framework.h"

/* 自动初始化函数（由链接器调用） */
__attribute__((constructor))
static void lib_init(void) {
    /* 框架的初始化将自动由src/main.c中的init函数处理 */
    /* 这里不需要额外的初始化 */
}

/* 自动清理函数（由链接器调用） */
__attribute__((destructor))
static void lib_cleanup(void) {
    /* 框架的清理将自动由src/main.c中的cleanup函数处理 */
    /* 这里不需要额外的清理 */
}

/**
 * 以下是标准钩子点的初始化，但不再提供默认实现
 * 这些只是预注册一些常用的钩子点，以便插件能够简单地使用它们
 * 这种设计类似于eBPF框架，它只定义了一些钩子点，但不提供任何具体行为
 */

#define REGISTER_HOOK(name, type, group) \
    void* orig_##name = get_original_function(#name); \
    if (orig_##name) { \
        register_hook_point(#name, type, group, orig_##name); \
    }

/* 预注册常用钩子点，但不提供默认实现 */
__attribute__((constructor))
static void register_common_hooks(void) {
    /* 文件操作钩子点 */
    REGISTER_HOOK(open, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(open64, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(close, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(read, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(write, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(pread, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(pwrite, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(fopen, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(fclose, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(fread, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(fwrite, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(unlink, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(rename, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(mkdir, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(rmdir, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(chmod, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(chown, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(stat, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(lstat, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(fstat, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(access, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(truncate, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    REGISTER_HOOK(ftruncate, HOOK_TYPE_LIBRARY, HOOK_GROUP_FILE_IO);
    
    /* 网络操作钩子点 */
    REGISTER_HOOK(socket, HOOK_TYPE_LIBRARY, HOOK_GROUP_NETWORK);
    REGISTER_HOOK(connect, HOOK_TYPE_LIBRARY, HOOK_GROUP_NETWORK);
    REGISTER_HOOK(bind, HOOK_TYPE_LIBRARY, HOOK_GROUP_NETWORK);
    REGISTER_HOOK(listen, HOOK_TYPE_LIBRARY, HOOK_GROUP_NETWORK);
    REGISTER_HOOK(accept, HOOK_TYPE_LIBRARY, HOOK_GROUP_NETWORK);
    REGISTER_HOOK(accept4, HOOK_TYPE_LIBRARY, HOOK_GROUP_NETWORK);
    REGISTER_HOOK(gethostbyname, HOOK_TYPE_LIBRARY, HOOK_GROUP_NETWORK);
    REGISTER_HOOK(getaddrinfo, HOOK_TYPE_LIBRARY, HOOK_GROUP_NETWORK);
    REGISTER_HOOK(send, HOOK_TYPE_LIBRARY, HOOK_GROUP_NETWORK);
    REGISTER_HOOK(sendto, HOOK_TYPE_LIBRARY, HOOK_GROUP_NETWORK);
    REGISTER_HOOK(recv, HOOK_TYPE_LIBRARY, HOOK_GROUP_NETWORK);
    REGISTER_HOOK(recvfrom, HOOK_TYPE_LIBRARY, HOOK_GROUP_NETWORK);
    REGISTER_HOOK(shutdown, HOOK_TYPE_LIBRARY, HOOK_GROUP_NETWORK);
    
    /* 进程操作钩子点 */
    REGISTER_HOOK(fork, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    REGISTER_HOOK(vfork, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    REGISTER_HOOK(clone, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    REGISTER_HOOK(execve, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    REGISTER_HOOK(execl, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    REGISTER_HOOK(execlp, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    REGISTER_HOOK(execle, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    REGISTER_HOOK(execv, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    REGISTER_HOOK(execvp, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    REGISTER_HOOK(system, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    REGISTER_HOOK(kill, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    REGISTER_HOOK(wait, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    REGISTER_HOOK(waitpid, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    REGISTER_HOOK(pipe, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    REGISTER_HOOK(popen, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    REGISTER_HOOK(pclose, HOOK_TYPE_LIBRARY, HOOK_GROUP_PROCESS);
    
    /* 内存操作钩子点 */
    REGISTER_HOOK(malloc, HOOK_TYPE_LIBRARY, HOOK_GROUP_MEMORY);
    REGISTER_HOOK(calloc, HOOK_TYPE_LIBRARY, HOOK_GROUP_MEMORY);
    REGISTER_HOOK(realloc, HOOK_TYPE_LIBRARY, HOOK_GROUP_MEMORY);
    REGISTER_HOOK(free, HOOK_TYPE_LIBRARY, HOOK_GROUP_MEMORY);
    REGISTER_HOOK(mmap, HOOK_TYPE_LIBRARY, HOOK_GROUP_MEMORY);
    REGISTER_HOOK(munmap, HOOK_TYPE_LIBRARY, HOOK_GROUP_MEMORY);
    REGISTER_HOOK(mprotect, HOOK_TYPE_LIBRARY, HOOK_GROUP_MEMORY);
    REGISTER_HOOK(brk, HOOK_TYPE_LIBRARY, HOOK_GROUP_MEMORY);
    REGISTER_HOOK(sbrk, HOOK_TYPE_LIBRARY, HOOK_GROUP_MEMORY);
    
    /* 加密操作钩子点 */
    REGISTER_HOOK(SSL_read, HOOK_TYPE_LIBRARY, HOOK_GROUP_CRYPTO);
    REGISTER_HOOK(SSL_write, HOOK_TYPE_LIBRARY, HOOK_GROUP_CRYPTO);
    REGISTER_HOOK(SSL_connect, HOOK_TYPE_LIBRARY, HOOK_GROUP_CRYPTO);
    REGISTER_HOOK(SSL_accept, HOOK_TYPE_LIBRARY, HOOK_GROUP_CRYPTO);
    REGISTER_HOOK(SSL_CTX_new, HOOK_TYPE_LIBRARY, HOOK_GROUP_CRYPTO);
    REGISTER_HOOK(SSL_CTX_free, HOOK_TYPE_LIBRARY, HOOK_GROUP_CRYPTO);
    REGISTER_HOOK(SSL_new, HOOK_TYPE_LIBRARY, HOOK_GROUP_CRYPTO);
    REGISTER_HOOK(SSL_free, HOOK_TYPE_LIBRARY, HOOK_GROUP_CRYPTO);
    
    /* 系统操作钩子点 */
    REGISTER_HOOK(uname, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(gethostname, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(getuid, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(geteuid, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(getgid, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(getegid, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(getpid, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(getppid, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(gettimeofday, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(clock_gettime, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(time, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(sysinfo, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(getcwd, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(chdir, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(getenv, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(setenv, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    REGISTER_HOOK(unsetenv, HOOK_TYPE_LIBRARY, HOOK_GROUP_SYSTEM);
    
    log_message(LOG_INFO, "已预注册常用钩子点");
} 